<template>
  <a-modal
    title="新增工序"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="工序名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入工序名称" v-decorator="['name', {rules: [{required: true, message: '请输入工序名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="不良品项列表"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select
            :showSearch="true"
            :filter-option="(input, option) =>{
              return option.componentOptions.children[0].elm.data.indexOf(input) >= 0
            }"
            mode="multiple"
            style="width: 100%"
            :maxTagCount="4"
            :maxTagTextLength="4"
            :allowClear="true"
            placeholder="请选择不良品项"
            v-decorator="['mulBadItem',{rules: [{ required: true, message: '请选择不良品项' }]}]">
            <a-select-option v-for="(item,index) in mulBadItemData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="报工权限"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <user-select
            mode="multiple"
            placeholder="请选择报工权限"
            v-decorator="['reportRight', { rules: [ {required: false, message: '请选择报工权限！' }]}]"/>
        </a-form-item>
        <a-form-item v-if="jsonAddShow">
          <json-add
            ref="jsonAdd"
            :fieldProp="fieldProp"
          />
        </a-form-item>
        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea placeholder="请输入备注" v-decorator="['remarks']" :auto-size="{ minRows: 3, maxRows: 6 }"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { workStepAdd } from '@/api/modular/main/workstep/workStepManage'
  import XEUtils from 'xe-utils'
  import JsonAdd from '@/components/MyComp/JsonAdd'
  import { fieldConfigList } from '@/api/modular/main/fieldconfig/fieldConfigManage'
  import UserSelect from '@/components/MyComp/UserSelect'
  export default {
    components: {
      JsonAdd,
      UserSelect
    },
    data () {
      return {
        fieldProp: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        jsonAddShow: true,
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        mulBadItemData: []

      }
    },
    methods: {
      // 初始化方法
      add (record) {
        this.visible = true
        this.dynamicFieldConfig()
          const dutyPersonOption = this.$options
          this.mulBadItemData = dutyPersonOption.filters['dictData']('bad_item')
      },
      // 动态字段赋值
      dynamicFieldConfig() {
        this.jsonAddShow = false
        this.fieldProp = {}
        fieldConfigList({ tableName: 'dw_work_step' }).then(res => {
          res.data.forEach(item => {
            // 查询自定义字段
            this.fieldProp[item.fieldIndex] = {
              type: item.fieldType,
              title: item.fieldTitle,
              'x-decorator': 'FormItem',
              // 'x-component': 'a-input',
              'x-decorator-props': {
                labelCol: 6,
                // labelWrap: true,
                wrapperCol: 16
              },
              'x-component-props': {
                placeholder: '请输入',
                style: {
                  width: '100%'
                }
              }
            }
          })
          this.jsonAddShow = true
        })
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            if (values.mulBadItem) {
              values.mulBadItem = values.mulBadItem.join(';')
            }
            if (values.reportRight) {
              values.reportRight = values.reportRight.join(';')
            }

            values.name = XEUtils.trim(values.name)
            values.code = XEUtils.trim(values.code)
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.code = ''
            this.$refs.jsonAdd.getAddForm().submit((josn) => {
              values.josn = josn
              workStepAdd(values).then((res) => {
                if (res.success) {
                  this.$message.success('新增成功')
                  this.confirmLoading = false
                  this.$emit('ok', values)
                  this.handleCancel()
                } else {
                  this.$message.error(res.message)// + res.message
                }
              }).finally((res) => {
                this.confirmLoading = false
              })
            })
          } else {
            this.confirmLoading = false
          }
        })
      },

      handleCancel () {
        this.$refs.jsonAdd.getAddForm().reset()
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
